package nmap

import (
	"bufio"
	"bytes"
	"gitee.com/wudicaidou/menciis-sirecog/services"
	"strconv"
	"strings"
	"sync"
)

var onc sync.Once

func GetDefaultServices() (defaultService map[int]string) {
	onc.Do(func() {
		defaultService = make(map[int]string)
		b := services.FSMustByte(false, "/nmap-services")
		scanner := bufio.NewScanner(bytes.NewReader(b))
		scanner.Split(bufio.ScanLines)
		for scanner.Scan() {
			line := scanner.Text()
			line = strings.TrimSpace(line)
			if line[0] == 0 || line[0] == '#' {
				continue
			}
			parts := strings.Fields(line)
			if len(parts) < 2 {
				continue
			}

			if parts[0] != "unknown" {
				portProtocol := strings.SplitN(parts[1], "/", 2)
				port, err := strconv.Atoi(portProtocol[0])
				if err != nil {
					continue
				}
				defaultService[port] = parts[0]
			}
		}
	})
	return defaultService
}
